package com.first.nell.info.idcard

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.*
import androidx.core.content.FileProvider
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.first.nell.R
import com.first.nell.base.BaseActivity
import com.first.nell.components.dialog.ImagePickerDialog
import com.first.nell.components.dialog.PopListItem
import com.first.nell.data.ReqImageUpload
import com.first.nell.data.ReqSaveImage
import com.first.nell.databinding.ActivityFirstNellIdentityCardBinding
import com.first.nell.enums.CardTypeEnum
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.*
import com.first.nell.launch.UserStateViewModel
import java.io.File


@SuppressLint("CustomSplashScreen")
class IdentityCardActivity : BaseActivity() {

    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellIdentityCardBinding

    //idcard
    private var mIdCardFrontIntent: Intent? = null
    private var mIdCardFrontCameraActivityResultLauncher: ActivityResultLauncher<Intent>? = null
    private var mIdCardFrontAlbumActivityResultLauncher: ActivityResultLauncher<String>? = null
    private var mIdCardFrontAlbumPickMedia: ActivityResultLauncher<PickVisualMediaRequest>? = null
    private var mIdCardFrontFilePath = ""

    private var mIdCardBackIntent: Intent? = null
    private var mIdCardBackCameraActivityResultLauncher: ActivityResultLauncher<Intent>? = null
    private var mIdCardBackAlbumActivityResultLauncher: ActivityResultLauncher<String>? = null
    private var mIdCardBackAlbumPickMedia: ActivityResultLauncher<PickVisualMediaRequest>? = null
    private var mIdCardBackFilePath = ""

    //drving lisence
    private var mDrivingFrontIntent: Intent? = null
    private var mDrivingFrontCameraActivityResultLauncher: ActivityResultLauncher<Intent>? = null
    private var mDrivingFrontAlbumActivityResultLauncher: ActivityResultLauncher<String>? = null
    private var mDrivingFrontAlbumPickMedia: ActivityResultLauncher<PickVisualMediaRequest>? = null
    private var mDrivingFrontFilePath = ""

    private var mDrivingBackIntent: Intent? = null
    private var mDrivingBackCameraActivityResultLauncher: ActivityResultLauncher<Intent>? = null
    private var mDrivingBackAlbumActivityResultLauncher: ActivityResultLauncher<String>? = null
    private var mDrivingBackAlbumPickMedia: ActivityResultLauncher<PickVisualMediaRequest>? = null
    private var mDrivingBackFilePath = ""

    //pass port
    private var mPassPortFrontIntent: Intent? = null
    private var mPassPortFrontCameraActivityResultLauncher: ActivityResultLauncher<Intent>? = null
    private var mPassPortFrontAlbumActivityResultLauncher: ActivityResultLauncher<String>? = null
    private var mPassPortFrontAlbumPickMedia: ActivityResultLauncher<PickVisualMediaRequest>? = null
    private var mPassPortFrontFilePath = ""

    private val mIdentityCardModel by lazy {
        ViewModelProvider(this).get(IdentityCardModel::class.java)
    }

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this).get(UserStateViewModel::class.java)
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mIdentityCardModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.ocr.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding =
            ActivityFirstNellIdentityCardBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //初始化拍照与选择图片回调
            registerTakePicActivityResultCallback()
            registerAlbumActivityResultCallback()

            //初始化下拉列表
            val list = mutableListOf(
                PopListItem(
                    display = getString(R.string.first_nell_idtype_idcard),
                    content = getString(R.string.first_nell_idtype_idcard)
                ),
                PopListItem(
                    display = getString(R.string.first_nell_idtype_passport),
                    content = getString(R.string.first_nell_idtype_passport)
                ),
                PopListItem(
                    display = getString(R.string.first_nell_idtype_drving),
                    content = getString(R.string.first_nell_idtype_drving)
                )
            )
            mFirstNellActivityFirstNellBinding.idcardFormSelectType.setFirstNellSourceFirstNellDataFirstNellList(
                list
            )
            mFirstNellActivityFirstNellBinding.idcardFormSelectType.setFirstNellOnFirstNellListFirstNellItemFirstNellClickFirstNellListener { display, content ->
                if (display.equals(getString(R.string.first_nell_idtype_idcard), true)) {
                    mFirstNellActivityFirstNellBinding.llIdcardNationBg.visibility = View.VISIBLE
                    mFirstNellActivityFirstNellBinding.llIdcardDrvingLicenseBg.visibility =
                        View.GONE
                    mFirstNellActivityFirstNellBinding.llIdcardPassPortBg.visibility = View.GONE
                    mIdentityCardModel.mFirstNellIdentityType = IdentityCardModel.IDENTITY_IDCARD

                } else if (display.equals(getString(R.string.first_nell_idtype_passport), true)) {
                    mFirstNellActivityFirstNellBinding.llIdcardNationBg.visibility = View.GONE
                    mFirstNellActivityFirstNellBinding.llIdcardPassPortBg.visibility = View.VISIBLE
                    mFirstNellActivityFirstNellBinding.llIdcardDrvingLicenseBg.visibility =
                        View.GONE
                    mIdentityCardModel.mFirstNellIdentityType = IdentityCardModel.IDENTITY_PASSPORT

                } else if (display.equals(getString(R.string.first_nell_idtype_drving), true)) {
                    mFirstNellActivityFirstNellBinding.llIdcardNationBg.visibility = View.GONE
                    mFirstNellActivityFirstNellBinding.llIdcardPassPortBg.visibility = View.GONE
                    mFirstNellActivityFirstNellBinding.llIdcardDrvingLicenseBg.visibility =
                        View.VISIBLE
                    mIdentityCardModel.mFirstNellIdentityType = IdentityCardModel.IDENTITY_DRIVING
                }
            }

            //上传图片回调
            mIdentityCardModel.onFirstNellUploadFirstNellImageFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let {
                        //保存图片url
                        if (it.type.equals(IdentityCardModel.IMAGE_IDCARD_FRONT, true)) {
                            mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront = it.url
                            mFirstNellActivityFirstNellBinding.imageIdcardNationFront.setImageBitmap(
                                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellFront
                            )
                        } else if (it.type.equals(IdentityCardModel.IMAGE_IDCARD_BACK, true)) {
                            mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellBack = it.url
                            mFirstNellActivityFirstNellBinding.imageIdcardNationBack.setImageBitmap(
                                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellBack
                            )
                        } else if (it.type.equals(IdentityCardModel.IMAGE_PASSPORT_FRONT, true)) {
                            mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront = it.url
                            mFirstNellActivityFirstNellBinding.imageIdcardPassPortFront.setImageBitmap(
                                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellFront
                            )
                        } else if (it.type.equals(IdentityCardModel.IMAGE_DRVING_FRONT, true)) {
                            mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront = it.url
                            mFirstNellActivityFirstNellBinding.imageIdcardDrvingLicenseFront.setImageBitmap(
                                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellFront
                            )
                        } else if (it.type.equals(IdentityCardModel.IMAGE_DRVING_BACK, true)) {
                            mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellBack = it.url
                            mFirstNellActivityFirstNellBinding.imageIdcardDrvingLicenseBack.setImageBitmap(
                                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellBack
                            )
                        }
                    }
                }
            }

            //保存图片回调
            mIdentityCardModel.onFirstNellSaveFirstNellImageFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    //获取流程状态
                    mUserStateViewModel.fetchFirstNellUserFirstNellState()
                }
            }

            //获取流程回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.ocr)
                }
            }

            //idcard
            mFirstNellActivityFirstNellBinding.imageIdcardNationFront.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_IDCARD_FRONT
                mFirstNellActivityFirstNellBinding.imageIdcardNationFront.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }
            mFirstNellActivityFirstNellBinding.imageIdcardNationBack.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_IDCARD_BACK
                mFirstNellActivityFirstNellBinding.imageIdcardNationBack.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }

            //passport
            mFirstNellActivityFirstNellBinding.imageIdcardPassPortFront.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_PASSPORT_FRONT
                mFirstNellActivityFirstNellBinding.imageIdcardPassPortFront.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }

            //driving
            mFirstNellActivityFirstNellBinding.imageIdcardDrvingLicenseFront.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_DRVING_FRONT
                mFirstNellActivityFirstNellBinding.imageIdcardDrvingLicenseFront.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }
            mFirstNellActivityFirstNellBinding.imageIdcardDrvingLicenseBack.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_DRVING_BACK
                mFirstNellActivityFirstNellBinding.imageIdcardDrvingLicenseBack.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }

            //commit
            mFirstNellActivityFirstNellBinding.submitInfo.setOnClickListener {
                mFirstNellActivityFirstNellBinding.submitInfo.recordFirstNellClickFirstNellEvent()

                //判断是否能提交
                var canSubmit = true
                if (mIdentityCardModel.mFirstNellIdentityType.equals(
                        IdentityCardModel.IDENTITY_IDCARD,
                        true
                    )
                ) {
                    if (mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront.isNotBlank() and mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellBack.isNotBlank()) {
                        //能提交
                        mIdentityCardModel.saveFirstNellImage(
                            ReqSaveImage(
                                cardImgUrl = mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront,
                                cardBackImgUrl = mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellBack,
                                cardType = CardTypeEnum.IDCARD.name.uppercase()
                            )
                        )

                    } else {
                        canSubmit = false
                    }
                } else if (mIdentityCardModel.mFirstNellIdentityType.equals(
                        IdentityCardModel.IDENTITY_PASSPORT,
                        true
                    )
                ) {
                    if (mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront.isNotBlank()) {
                        //能提交
                        mIdentityCardModel.saveFirstNellImage(
                            ReqSaveImage(
                                cardImgUrl = mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront,
                                cardType = CardTypeEnum.PASSPORT.name.uppercase()
                            )
                        )
                    } else {
                        canSubmit = false
                    }
                } else if (mIdentityCardModel.mFirstNellIdentityType.equals(
                        IdentityCardModel.IDENTITY_DRIVING,
                        true
                    )
                ) {
                    if (mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront.isNotBlank() and mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellBack.isNotBlank()) {
                        //能提交
                        mIdentityCardModel.saveFirstNellImage(
                            ReqSaveImage(
                                cardImgUrl = mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront,
                                cardBackImgUrl = mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellBack,
                                cardType = CardTypeEnum.DRIVING.name.uppercase()
                            )
                        )
                    } else {
                        canSubmit = false
                    }
                }

                if (!canSubmit) {
                    Toast.makeText(
                        this,
                        "Please select picture first.",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            // 默认第1个
            mFirstNellActivityFirstNellBinding.idcardFormSelectType.setFirstNellContent("National ID card#National ID card")
            mFirstNellActivityFirstNellBinding.llIdcardNationBg.visibility = View.VISIBLE
            mFirstNellActivityFirstNellBinding.llIdcardDrvingLicenseBg.visibility = View.GONE
            mFirstNellActivityFirstNellBinding.llIdcardPassPortBg.visibility = View.GONE
            mIdentityCardModel.mFirstNellIdentityType = IdentityCardModel.IDENTITY_IDCARD

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()

        return notFillOptions.joinToString("#")
    }

    override fun onFirstNellPermissionsFirstNellRequestFirstNellAllFirstNellPass() {
        if (mIdentityCardModel.mFirstNellImageType.equals(
                IdentityCardModel.IMAGE_IDCARD_FRONT,
                true
            )
        ) {
            ImagePickerDialog.showDialog(supportFragmentManager) { action ->
                if (0 == action) {
                    mIdCardFrontCameraActivityResultLauncher?.launch(mIdCardFrontIntent)
                } else {
                    mIdCardFrontAlbumPickMedia?.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
                }
            }

        } else if (mIdentityCardModel.mFirstNellImageType.equals(
                IdentityCardModel.IMAGE_IDCARD_BACK,
                true
            )
        ) {
            ImagePickerDialog.showDialog(supportFragmentManager) { action ->
                if (0 == action) {
                    mIdCardBackCameraActivityResultLauncher?.launch(mIdCardBackIntent)
                } else {
                    mIdCardBackAlbumPickMedia?.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
                }
            }

        } else if (mIdentityCardModel.mFirstNellImageType.equals(
                IdentityCardModel.IMAGE_PASSPORT_FRONT,
                true
            )
        ) {
            ImagePickerDialog.showDialog(supportFragmentManager) { action ->
                if (0 == action) {
                    mPassPortFrontCameraActivityResultLauncher?.launch(mPassPortFrontIntent)
                } else {
                    mPassPortFrontAlbumPickMedia?.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
                }
            }

        } else if (mIdentityCardModel.mFirstNellImageType.equals(
                IdentityCardModel.IMAGE_DRVING_FRONT,
                true
            )
        ) {
            ImagePickerDialog.showDialog(supportFragmentManager) { action ->
                if (0 == action) {
                    mDrivingFrontCameraActivityResultLauncher?.launch(mDrivingFrontIntent)
                } else {
                    mDrivingFrontAlbumPickMedia?.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
                }
            }

        } else if (mIdentityCardModel.mFirstNellImageType.equals(
                IdentityCardModel.IMAGE_DRVING_BACK,
                true
            )
        ) {
            ImagePickerDialog.showDialog(supportFragmentManager) { action ->
                if (0 == action) {
                    mDrivingBackCameraActivityResultLauncher?.launch(mDrivingBackIntent)
                } else {
                    mDrivingBackAlbumPickMedia?.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
                }
            }
        }
    }

    /** 注册拍照回调 */
    @SuppressLint("Recycle", "Range")
    private fun registerTakePicActivityResultCallback() {
        mIdCardFrontFilePath = getFirstNellCacheFirstNellImageFirstNellFileFirstNellPath("icfront")
        val idcardFrontFile = File(mIdCardFrontFilePath)
        val mIdCardFrontImageUri: Uri = if (Build.VERSION.SDK_INT < 24) {
            Uri.fromFile(idcardFrontFile)
        } else {
            FileProvider.getUriForFile(this, "$packageName.file_provider", idcardFrontFile)
        }
        mIdCardFrontIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
            putExtra(MediaStore.EXTRA_OUTPUT, mIdCardFrontImageUri)
        }
        mIdCardFrontCameraActivityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellCameraFirstNellActivity(
                        outFilePath = mIdCardFrontFilePath,
                        result = result,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

        mIdCardBackFilePath = getFirstNellCacheFirstNellImageFirstNellFileFirstNellPath("icback")
        val idcardBackFile = File(mIdCardBackFilePath)
        val mIdCardBackImageUri: Uri = if (Build.VERSION.SDK_INT < 24) {
            Uri.fromFile(idcardBackFile)
        } else {
            FileProvider.getUriForFile(this, "$packageName.file_provider", idcardBackFile)
        }
        mIdCardBackIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
            putExtra(MediaStore.EXTRA_OUTPUT, mIdCardBackImageUri)
        }
        mIdCardBackCameraActivityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellCameraFirstNellActivity(
                        outFilePath = mIdCardBackFilePath,
                        result = result,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

        //////////////////////////
        mDrivingFrontFilePath = getFirstNellCacheFirstNellImageFirstNellFileFirstNellPath("icfront")
        val drivingFrontFile = File(mDrivingFrontFilePath)
        val mDrivingFrontImageUri: Uri = if (Build.VERSION.SDK_INT < 24) {
            Uri.fromFile(drivingFrontFile)
        } else {
            FileProvider.getUriForFile(this, "$packageName.file_provider", drivingFrontFile)
        }
        mDrivingFrontIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
            putExtra(MediaStore.EXTRA_OUTPUT, mDrivingFrontImageUri)
        }
        mDrivingFrontCameraActivityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellCameraFirstNellActivity(
                        outFilePath = mDrivingFrontFilePath,
                        result = result,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

        mDrivingBackFilePath = getFirstNellCacheFirstNellImageFirstNellFileFirstNellPath("icback")
        val drivingBackFile = File(mDrivingBackFilePath)
        val mDrivingBackImageUri: Uri = if (Build.VERSION.SDK_INT < 24) {
            Uri.fromFile(drivingBackFile)
        } else {
            FileProvider.getUriForFile(this, "$packageName.file_provider", drivingBackFile)
        }
        mDrivingBackIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
            putExtra(MediaStore.EXTRA_OUTPUT, mDrivingBackImageUri)
        }
        mDrivingBackCameraActivityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellCameraFirstNellActivity(
                        outFilePath = mDrivingBackFilePath,
                        result = result,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

        ///////////////////////
        mPassPortFrontFilePath =
            getFirstNellCacheFirstNellImageFirstNellFileFirstNellPath("icfront")
        val passPortFrontFile = File(mPassPortFrontFilePath)
        val mPassPortFrontImageUri: Uri = if (Build.VERSION.SDK_INT < 24) {
            Uri.fromFile(passPortFrontFile)
        } else {
            FileProvider.getUriForFile(this, "$packageName.file_provider", passPortFrontFile)
        }
        mPassPortFrontIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
            putExtra(MediaStore.EXTRA_OUTPUT, mPassPortFrontImageUri)
        }
        mPassPortFrontCameraActivityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellCameraFirstNellActivity(
                        outFilePath = mPassPortFrontFilePath,
                        result = result,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
    }

    /** 注册相册回调 */
    private fun registerAlbumActivityResultCallback() {
        try {
            mIdCardFrontAlbumPickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                        result = uri,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            mIdCardBackAlbumPickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                        result = uri,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            mDrivingFrontAlbumPickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                        result = uri,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            mDrivingBackAlbumPickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                        result = uri,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            mPassPortFrontAlbumPickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
                try {
                    pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                        result = uri,
                        convertFinish = { covetPath, bitmap ->
                            onHandlerImagePickResult(covetPath, bitmap)
                        })
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            mIdCardFrontAlbumActivityResultLauncher =
                registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
                    try {
                        pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                            result = uri,
                            convertFinish = { covetPath, bitmap ->
                                onHandlerImagePickResult(covetPath, bitmap)
                            })
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

            mIdCardBackAlbumActivityResultLauncher =
                registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
                    try {
                        try {
                            pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                                result = uri,
                                convertFinish = { covetPath, bitmap ->
                                    onHandlerImagePickResult(covetPath, bitmap)
                                })
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

            mDrivingFrontAlbumActivityResultLauncher =
                registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
                    try {
                        try {
                            pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                                result = uri,
                                convertFinish = { covetPath, bitmap ->
                                    onHandlerImagePickResult(covetPath, bitmap)
                                })
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

            mDrivingBackAlbumActivityResultLauncher =
                registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
                    try {
                        try {
                            pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                                result = uri,
                                convertFinish = { covetPath, bitmap ->
                                    onHandlerImagePickResult(covetPath, bitmap)
                                })
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

            mPassPortFrontAlbumActivityResultLauncher =
                registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
                    try {
                        try {
                            pickFirstNellResultFirstNellFromFirstNellAlbumFirstNellActivity(
                                result = uri,
                                convertFinish = { covetPath, bitmap ->
                                    onHandlerImagePickResult(covetPath, bitmap)
                                })
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 处理选择/拍照回调 */
    private fun onHandlerImagePickResult(covetPath: String?, bitmap: Bitmap?) {
        if (covetPath?.isNotBlank() == true) {
            if (mIdentityCardModel.mFirstNellImageType.endsWith("FRONT")) {
                mIdentityCardModel.mFirstNellImageFirstNellPathFirstNellFront = covetPath
                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellFront = bitmap
            } else {
                mIdentityCardModel.mFirstNellImageFirstNellPathFirstNellBack = covetPath
                mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellBack = bitmap
            }

            //上传图片
            mIdentityCardModel.uploadFirstNellImage(
                ReqImageUpload(
                    filePath = covetPath,
                    cardType = mIdentityCardModel.mFirstNellImageType
                )
            )
        } else {
            Toast.makeText(
                this,
                "Please select picture first.",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    /** 请求相机权限 */
    private fun requestExtPermissoins() {
        requestFirstNellSpecFirstNellPermissions(arrayOf(android.Manifest.permission.CAMERA))
    }
}